package com.noble.meether.mail.ui.friend;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.ExBaseAdapter;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.image.ImageLoader;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.ui.dialog.MsgTipDialog;

import java.util.List;

/**
 * Created by zhang on 2017/5/4.
 */
public class MyDefriendAdapter extends ExBaseAdapter<UserLight> implements RequestComplete {
    private UserLight userInfo;

    public MyDefriendAdapter(Context context, List<UserLight> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.f1_my_defriend_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        userInfo = getItem(position);
        ImageLoader.loadCircleAvatar(getContext(), userInfo.getAvatar(), holder.iv_head);
        holder.user_nick.setText(userInfo.getNickname());
        holder.user_vip.setVisibility(userInfo.isVip() ? View.VISIBLE : View.GONE);

        holder.cancel_black.setOnClickListener(view -> MeetUIShow.showMsgTipDlg((FragmentActivity) App.activity,
                "确定将该用户从黑名单移除吗?", new MsgTipDialog.OnDialogClickListener() {
                    @Override
                    public void onSubmit() {
                        LoadingDialog.show((FragmentActivity) App.activity, "请稍后...");
                        ModuleMgr.getCommonMgr().setRemoveBlackList(userInfo.getUidTo(), MyDefriendAdapter.this);
                    }

                    @Override
                    public void onCancel() {
                    }
                }));
        return convertView;
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        LoadingDialog.closeLoadingDialog(200);
        if (!response.isOk()) return;
        try {
            getList().remove(userInfo);
            notifyDataSetChanged();
        } catch (Exception e) {
        }
    }

    private class MyViewHolder {
        private ImageView iv_head, cancel_black, user_vip;
        private TextView user_nick;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            iv_head = convertView.findViewById(R.id.iv_head);
            user_nick = convertView.findViewById(R.id.user_nick);
            cancel_black = convertView.findViewById(R.id.cancel_black);
            user_vip = convertView.findViewById(R.id.user_vip);
        }
    }
}
